#!/usr/bin/perl
# index.cgi
# display the system monitor information including cpu temperature,fan speed, voltage,etc.

do '../web-lib.pl';

&init_config();

&header($text{'index_title'},"","index",1,1,0,undef,"<meta http-equiv=refresh content=30>");
#print "<hr>";

my $result = `cat /root/sensors 2>/dev/null`;

if($result =~ /No such file or directory/)
{
	&nas_syslog("sysmon","naslog_warning",14000,"the sysmon module is not installed!");
	`health_load`;
}

my @entry = split('\n',$result);
my @entry = get_sysmon();
my %elem;

foreach my $i (@entry)
{
	my ($key,$value) = split(':',$i);
	$elem{$key} =$value;
#        print $key.$value."|";
}

print "<table class=mode1 width=75% border=0 cellspacing=0 cellpadding=10>";

print "<tr><td><img src='images/temp.gif'></td><td>";
print "<table class=mode1 width=75% border=0 cellspacing=0 cellpadding=2>";
print "<tr class=title1><td colspan=3><b><i>$text{'temperature'}</i></b></td></tr>";
print "<tr class=mode1><td width=33%><b>$text{'cpu'}</b></td><td width=33%><b>$text{'chassis'}</b></td></tr>";
print "<tr class=mode2>";
#foreach my $i ("CPU Temp","Chassis Temp","Other Temp")
foreach my $i ("CPU","DDR")
{
        #$elem{$i} = ($elem{$i} - 90)."C";
	print "<td>$elem{$i} C</td>";
}
print "</tr>";
print "</table></td></tr>";


#print "<tr><td width=7%><img src='images/fan.gif'></td><td>";
#print "<table class=mode1 width=75% border=0 cellspacing=0 cellpadding=2>";
#print "<tr class=title1><td colspan=3><b><i>$text{'fan_speed'}</i></b></td></tr>";
#print "<tr class=mode1><td width=33%><b>$text{'cpu'}</b></td><td width=33%><b>$text{'chassis'}</b></td></tr>";
#print "<tr class=mode2>";
#foreach my $i ("CPU Fan RPM","Chassis Fan RPM","Other Fan RPM")
#foreach my $i ("CPU Fan RPM","Chassis Fan RPM")
#{
#	print "<td>$elem{$i}</td>";
#}
#print "</tr>";
#print "</table></td></tr>";


print "<tr><td><img src='images/voltage.gif'></td><td>";
print "<table class=mode1 width=75% border=0 cellspacing=0 cellpadding=2>";
print "<tr class=title1><td colspan=7><b><i>$text{'voltage'}</i></b></td></tr>";
print "<tr class=mode1><td><b>5V StandBy</b></td><td><b>CPU Voltage</b></td></tr>";
print "<tr class=mode2>";
#foreach my $i ("Vcore","+1.5V","+3.3V","+5.0V","-5.0V","+12.0V","-12.0V")
foreach my $i ("5V_StandBy","CPU_Voltage")
{
	print "<td>$elem{$i}</td>";
}
print "</tr>";
print "</table></td></tr>";


print "</table>";

#print "<hr>";

&footer("/",$text{'index'});
sub get_sysmon()
{
        open(FH, "/usr/bin/sensors |");
        @sen = <FH>;
        close(FH);
        my $sysmon;
        for($i=0; $i<@sen; $i++) {
                $sen[$i] =~ s/\r|\n//g;
                $sen[$i] =~ s/\*|\t//g;
                if($sen[$i] =~ /(\+3\.3V:)\s+(\S+)\sV/){
                        push(@sysmon, $1.$2);
                }
                elsif($sen[$i] =~ /(5V StandBy:)/) {
			if($sen[$i+1] =~ /\s+(\S+)\sV/){
                        	push(@sysmon,"5V_StandBy:".$1);
			}
                }
                elsif($sen[$i] =~ /(CPU Voltage:)/){
			if($sen[$i+1] =~ /\s+(\S+)\sV/){
                        	push(@sysmon,"CPU_Voltage:".$1);
			}
                }
                elsif($sen[$i] =~ /(\+5V:)\s+(\S+)\sV/){
                        push(@sysmon,$1.$2);
                }
                elsif($sen[$i] =~ /(\+12V:)\s+(\S+)\sV/){
                        push(@sysmon,$1.$2);
                }
                elsif($sen[$i] =~ /(-12V:)\s+(\S+)\sV/){
                        push(@sysmon,$1.$2);
                }
                elsif($sen[$i] =~ /(Vdd:)\s+(\S+)\sV/){
                        push(@sysmon,$1.$2);
                }
                elsif($sen[$i] =~ /(Vbat:)\s+(\S+)\sV/){
                        push(@sysmon,$1.$2);
                }
                elsif($sen[$i] =~ /(Vsb:)\s+(\S+)\sV/){
                        push(@sysmon,$1.$2);
                }
                elsif($sen[$i] =~ /(AVdd:)\s+(\S+)\sV/){
                        push(@sysmon,$1.$2);
                }
                elsif($sen[$i] =~ /(CPU0 Temp:)\s*(\+\d+\.*\d*)/){
                        push(@sysmon,"CPU:".$2);
                }
                elsif($sen[$i] =~ /(S-IO Temp:)\s*(\+\d+\.*\d*)/){
                       	push(@sysmon,"S-IO:".$2);
                }
                elsif($sen[$i] =~ /(DDR Temp:)\s*(\+\d+\.*\d*)/){
                        push(@sysmon,"DDR:".$2);
                }
        }
        return @sysmon;
}
